package com.tuanzi.loan.business.service.product.product.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.product.product.LoanApiFicoTagRecord;
import com.tuanzi.loan.business.entity.product.product.LoanProductApiFicoTag;
import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.repository.product.product.LoanProductApiFicoTagRepository;
import com.tuanzi.loan.business.repository.product.product.custom.LoanApiFicoTagRecordRepositoryCustom;
import com.tuanzi.loan.business.service.product.product.LoanApiFicoTagRecordService;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.LoanProductUserTag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chenweijun
 * @Date: 2019/8/30 11:33
 */
@Slf4j
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class LoanApiFicoTagRecordServiceImpl implements LoanApiFicoTagRecordService{

    @Autowired
    private LoanApiFicoTagRecordRepositoryCustom loanApiFicoTagRecordRepositoryCustom;
    @Autowired
    private LoanProductApiFicoTagRepository loanProductApiFicoTagRepository;
    @Autowired
    private DictionaryService dictionaryService;

    @Override
    public List<LoanProductUserTag> findUserTags() {

        List<LoanProductUserTag> loanProductUserTags = Lists.newArrayList();

        List<LoanApiFicoTagRecord> loanApiFicoTagRecords =  loanApiFicoTagRecordRepositoryCustom.findDistinctTags();

        loanProductUserTags = loanApiFicoTagRecords.stream().map(loanApiFicoTagRecord -> {
            LoanProductUserTag loanProductUserTag = new LoanProductUserTag();
            loanProductUserTag.setValue(loanApiFicoTagRecord.getTags());
            loanProductUserTag.setLabel("faco-" + loanApiFicoTagRecord.getTags());
            return loanProductUserTag;
        }).collect(Collectors.toList());

        List<Dictionary> dictionaries =  dictionaryService.findAllByCode(DictionaryCode.LOAN_PRODUCT_USER_TAG);
        loanProductUserTags.addAll( dictionaries.stream().map(dictionary -> {
            LoanProductUserTag loanProductUserTag = new LoanProductUserTag();
            loanProductUserTag.setValue(dictionary.getValue1());
            loanProductUserTag.setLabel(dictionary.getName());
            return loanProductUserTag;
        }).collect(Collectors.toList()));

        LoanProductUserTag loanProductUserTag = new LoanProductUserTag();
        loanProductUserTag.setValue("未知");
        loanProductUserTag.setLabel("未知");
        loanProductUserTags.add(0, loanProductUserTag);

        return loanProductUserTags;
    }

    @Override
    public LoanProductApiFicoTag findProductApiFicoTagByProductId(Long productId) {
        return loanProductApiFicoTagRepository.findOneByProductId(productId);
    }


}
